//
//  QSDrawerViewController.m
//  抽屉视图demo
//
//  Created by apple on 2017/1/5.
//  Copyright © 2017年 Song. All rights reserved.
//

#import "QSDrawerViewController.h"
#import "QSBaseTool.h"
#import "UIView+QSFrame.h"

@interface QSDrawerViewController ()

@end

@implementation QSDrawerViewController
#pragma mark - get方法，设置默认值

#pragma mark - set方法
/**
 *  中间view距离上下的距离
 */
- (void)setMaxY:(CGFloat)maxY
{
    _maxY = maxY;
}

/**
 *  中间view向右的偏移距离
 */
- (void)setOffsetRight:(CGFloat)offsetRight
{
    _offsetRight = offsetRight;
}

/**
 *  中间view向左的偏移距离
 */
- (void)setOffsetLeft:(CGFloat)offsetLeft
{
    _offsetLeft = offsetLeft;
}

/**
 *  初始化方法
 */
- (instancetype)initWithSlidingDirection:(QSSlidingDirectionStyle)slidingDirection offsetRight:(CGFloat)offsetRight offsetLeft:(CGFloat)offsetLeft andMaxY:(CGFloat)maxY
{
    if (self = [super init]) {
        self.slidingDirection = slidingDirection;
        self.offsetLeft = offsetLeft;
        self.offsetRight = offsetRight;
        self.maxY = maxY;
        
        // 添加views
        [self setupViews];
        
        // 添加手势
        [self setupGesture];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

/**
 *  添加手势
 */
- (void)setupGesture
{
    // 给中间的view添加边缘滑动手势
    switch (self.slidingDirection) {
        case QSSlidingDirectionStyleLeftAndRight:{
            UIScreenEdgePanGestureRecognizer* screenEdgePanLeft = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
            screenEdgePanLeft.edges = UIRectEdgeLeft;
            [self.centerView addGestureRecognizer:screenEdgePanLeft];
            
            UIScreenEdgePanGestureRecognizer* screenEdgePanRight = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
            screenEdgePanRight.edges = UIRectEdgeRight;
            [self.centerView addGestureRecognizer:screenEdgePanRight];
        }
            break;
            
        case QSSlidingDirectionStyleLeft:{
            UIScreenEdgePanGestureRecognizer* screenEdgePanLeft = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
            screenEdgePanLeft.edges = UIRectEdgeLeft;
            [self.centerView addGestureRecognizer:screenEdgePanLeft];
        }
            break;
            
        case QSSlidingDirectionStyleRight:{
            UIScreenEdgePanGestureRecognizer* screenEdgePanRight = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
            screenEdgePanRight.edges = UIRectEdgeRight;
            [self.centerView addGestureRecognizer:screenEdgePanRight];
        }
            break;
    }
    
    // 给控制器的view添加点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes)];
    [self.view addGestureRecognizer:tap];
}

/**
 *  点击手势回调方法
 */
- (void)tapGes
{
    // 复位操作
    [UIView animateWithDuration:0.5 animations:^{
        self.centerView.frame = self.view.bounds;
    }];
}

/**
 *  滑动手势回调方法
 */
- (void)panGes:(UIScreenEdgePanGestureRecognizer *)pan
{
    // 获取手指的偏移量
    CGPoint transP = [pan translationInView:self.centerView];
    
    // 直接修改中间view的frame
    self.centerView.frame = [self frameWithOffsetX:transP.x];
    
    // 判断中间view的x值：大于0，显示左边view的内容；小于0，显示右边的内容
    if (self.centerView.x > 0) {
        // 隐藏右边的view
        self.rightView.hidden = YES;
    } else if (self.centerView.x < 0){
        // 显示右边的view
        self.rightView.hidden = NO;
    }
    
    // 判断手势的状态，当手指离开时，定位手指离开的位置
    CGFloat target = 0.0;
    if (pan.state == UIGestureRecognizerStateEnded) {
        // 当中间view的x大于屏幕宽度的一般时，显示左边
        if (self.centerView.x > self.offsetRight) {
            target = self.offsetRight;
        } else if (CGRectGetMaxX(self.centerView.frame) < (kScreenWidth + self.offsetLeft)) {
            // 当中间view的最大x值小于屏幕宽度的一半时，显示右边
            target = self.offsetLeft;
        }
        
        CGFloat offsetX = target - self.centerView.x;
        // 设置动画
        [UIView animateWithDuration:0.5 animations:^{
            self.centerView.frame = [self frameWithOffsetX:offsetX];
        }];
    }
    
    // 复位
    [pan setTranslation:CGPointZero inView:self.centerView];
}

/**
 *  根据一个偏移量计算中间view的frame
 */
- (CGRect)frameWithOffsetX:(CGFloat)offsetX
{
    CGRect frame = self.centerView.frame;
    frame.origin.x += offsetX;
    
    // 计算y值 (因为向左滑动的时候计算结果是负数，所以这里取绝对值)
    frame.origin.y = fabs(frame.origin.x / kScreenWidth * self.maxY);
    
    // 计算高度
    frame.size.height = kScreenHeight - 2 * frame.origin.y;
    
    return frame;
}

/**
 *  添加views
 */
- (void)setupViews
{
    // 添加左边的view
    UIView *leftV = [[UIView alloc] initWithFrame:self.view.bounds];
    _leftView = leftV;
    [self.view addSubview:leftV];
    
    // 添加右边的view
    UIView *rightV = [[UIView alloc] initWithFrame:self.view.bounds];
    _rightView = rightV;
    [self.view addSubview:rightV];
    
    // 最后在添加中间的view
    UIView *centerV = [[UIView alloc] initWithFrame:self.view.bounds];
    _centerView = centerV;
    [self.view addSubview:centerV];
}
@end
